// ====================Game_Card====================

/**
 * 卡牌对象
 */
class Game_Card {
    constructor(cardId) {
        this.initialize(cardId);
    }

    initialize(cardId) {
        const data = $dataCards[cardId];
        if (!data) throw "卡牌数据不存在";
        const {id, skillId, name, cost, damage, armor, actionSeq, requirement} = data;
        this._id = id || 0;
        this._skillId = skillId || 0;
        this._name = name || "";
        this._cost = cost || 0;
        this._damage = damage || 0;
        this._armor = armor || 0;
        this._type = data.type || [];
        this._actionSeq = actionSeq || [{name: "行动", args: []}];
        this._lock = false;
        this._requirement = requirement || true;
        this._target = -1;
    }

    id() {
        return this._id;
    }

    skillId() {
        return this._skillId;
    }

    name() {
        return this._name;
    }

    description() {
        return $dataSkills[this.skillId()]?.description;
    }

    cost() {
        return this._cost;
    }

    damage() {
        if (this._damage > 0) {
            return $gameParty.battleMembers()[0].atk + this._damage;
        }
        return this._damage;
    }

    armor() {
        return this._armor;
    }

    type() {
        return this._type;
    }

    actionSeq() {
        return this._actionSeq;
    }

    requirement() {
        return this._requirement;
    }

    animationId() {
        return $dataSkills[this.skillId()]?.animationId;
    }

    setTarget(target) {
        this._target = target;
    }

    target() {
        return this._target;
    }

    meetRequirement() {
        const requirement = this.requirement();
        if(!requirement) return true;
        const card = this;
        const args = [card];
        const func = new Function("card", `return ${requirement}`);
        // console.log(func)
        let result = false;
        try {
            result = func(...args);
        } catch (e) {
            console.error(`卡牌自定义条件代码错误：${requirement}\n卡牌id：${this.id()}`);
        }
        return result === true;
    }

    changeCost(value) {
        this._cost = Math.max(0, this._cost + value);
    }

    changeDamage(value) {
        if (this._damage > 0) {
            this._damage = Math.max(0, this._damage + value);
        }
        if (this._damage < 0) {
            this._damage = Math.min(0, this._damage + value);
        }
    }

    isType(type) {
        return this._type.includes(type);
    }

    reset() {
        const cardId = this.id();
        this.initialize(cardId);
    }

    lock() {
        this._lock = true;
    }

    unlock() {
        this._lock = false;
    }

    isLock() {
        return this._lock;
    }
}